﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CUiExportToRTRContentAllRobotsCollisionSetsDataGenerator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

internal class CUiExportToRTRContentAllRobotsCollisionSetsDataGenerator
{
  private CUiExportToRTRContentDataHandler _dataHandler;
  private CApExportToRTR3DData _rtr3DData;
  private Dictionary<ITxObject, TxRobot> _objectsWith3DOfRobot = new Dictionary<ITxObject, TxRobot>();
  private HashSet<ITxObject> _objectsWith3D = new HashSet<ITxObject>();

  internal CUiExportToRTRContentAllRobotsCollisionSetsDataGenerator(
    CUiExportToRTRContentDataHandler dataHandler)
  {
    this._dataHandler = dataHandler;
    this._rtr3DData = dataHandler.RTR3DData;
  }

  internal void Execute()
  {
    this.AccumulateAllNonRobotAsStaticObjects();
    this.Compute3DMapOfRobotsAndStatic();
    this.ComputeCollisionSets();
    this.ComputeRobotWithCollissionSetsOwnCollisionSets();
  }

  private void ComputeCollisionSets()
  {
    foreach (TxCollisionPair pair in (Collection<ITxObject>) TxApplication.ActiveDocument.CollisionRoot.PairList)
    {
      if (pair.Active)
        new CUiExportToRTRContentRobotCollisionSetsDataGenerator(this._objectsWith3DOfRobot, this._objectsWith3D, pair, this._dataHandler.CollisionSetsData).Execute();
    }
  }

  private void AccumulateAllNonRobotAsStaticObjects()
  {
    foreach (KeyValuePair<ITxObject, CApExportToRTR3DData.ObjectExport3DData> keyValuePair in this._rtr3DData.ObjectWithSubObjectToExport)
      this._objectsWith3D.Add(keyValuePair.Key);
  }

  private void Compute3DMapOfRobotsAndStatic()
  {
    foreach (KeyValuePair<TxRobot, List<ITxObject>> keyValuePair in this._rtr3DData.RobotObjectsToExport)
    {
      TxRobot key = keyValuePair.Key;
      foreach (ITxObject itxObject in keyValuePair.Value)
        this.Compute3DMapOfRobotAndObjects(keyValuePair.Key, itxObject);
    }
  }

  private void Compute3DMapOfRobotAndObjects(TxRobot robot, ITxObject obj)
  {
    this._objectsWith3DOfRobot.Add(obj, robot);
    this._objectsWith3D.Remove(obj);
    CApExportToRTR3DData.ObjectExport3DData objectExport3Ddata;
    if (!this._rtr3DData.ObjectWithSubObjectToExport.TryGetValue(obj, out objectExport3Ddata))
      return;
    foreach (ITxObject itxObject in objectExport3Ddata.SeparateFromParent3D)
      this.Compute3DMapOfRobotAndObjects(robot, itxObject);
  }

  private void ComputeRobotWithCollissionSetsOwnCollisionSets()
  {
    foreach (TxRobot key in this._dataHandler.CollisionSetsData.Keys)
      this.ComputeRobotOwnCollissionSets(key);
  }

  private void ComputeRobotOwnCollissionSets(TxRobot robot)
  {
    HashSet<ITxLocatableObject> ignoredLinks = new HashSet<ITxLocatableObject>();
    List<ITxObject> itxObjectList;
    if (!this._rtr3DData.RobotObjectsToExport.TryGetValue(robot, out itxObjectList))
      return;
    List<ITxLocatableObject> itxLocatableObjectList1 = new List<ITxLocatableObject>();
    List<ITxLocatableObject> filteredRobotList = new List<ITxLocatableObject>();
    List<ITxLocatableObject> itxLocatableObjectList2 = new List<ITxLocatableObject>();
    foreach (ITxLocatableObject key in itxObjectList)
    {
      if (key is TxKinematicLink)
      {
        itxLocatableObjectList1.Add(key);
      }
      else
      {
        CApExportToRTR3DData.ObjectExport3DData objectExport3Ddata;
        if (this._rtr3DData.ObjectWithSubObjectToExport.TryGetValue((ITxObject) key, out objectExport3Ddata) && objectExport3Ddata.IsWithKinematics)
        {
          this.AddIgnoreLinkOfObject(key.AttachmentParent, ignoredLinks);
          itxLocatableObjectList2.Add(key);
          this.AddListAndSubList(objectExport3Ddata.SeparateFromParent3D, itxLocatableObjectList2);
        }
      }
    }
    CApExportToRTRRobotCollisionSetsData collisionSetsData = this._dataHandler.CollisionSetsData[(ITxRobot) robot];
    itxLocatableObjectList1.ForEach((Action<ITxLocatableObject>) (ownObj =>
    {
      if (ignoredLinks.Contains(ownObj))
        return;
      filteredRobotList.Add(ownObj);
    }));
    if (filteredRobotList.Count <= 0 || itxLocatableObjectList2.Count <= 0)
      return;
    collisionSetsData.AddRobotOwnCollisionSets(filteredRobotList, itxLocatableObjectList2);
  }

  private void AddIgnoreLinkOfObject(
    ITxLocatableObject obj,
    HashSet<ITxLocatableObject> ignoredLinks)
  {
    if (obj is TxKinematicLink)
      ignoredLinks.Add(obj);
    else if (obj.AttachmentParent is TxKinematicLink attachmentParent)
    {
      ignoredLinks.Add((ITxLocatableObject) attachmentParent);
    }
    else
    {
      if (!(((ITxObject) obj).Collection is TxKinematicLink collection))
        return;
      ignoredLinks.Add((ITxLocatableObject) collection);
    }
  }

  private void AddListAndSubList(List<ITxObject> objectList, List<ITxLocatableObject> collisionList)
  {
    foreach (ITxLocatableObject key in objectList)
    {
      collisionList.Add(key);
      CApExportToRTR3DData.ObjectExport3DData objectExport3Ddata;
      if (this._rtr3DData.ObjectWithSubObjectToExport.TryGetValue((ITxObject) key, out objectExport3Ddata))
        this.AddListAndSubList(objectExport3Ddata.SeparateFromParent3D, collisionList);
    }
  }
}
